//
//  personal_B0.m
//  K-Music
//
//  Created by CHUN on 16/1/15.
//  Copyright © 2016年 CHUN. All rights reserved.
//

#import "personal_B0.h"
#import "UpUserIconControllerViewController.h"
#import "QiniuSDK.h"
@interface personal_B0 ()<UITableViewDataSource ,UITableViewDelegate>
{
    UIImageView *iconImage;
    UISwitch *swich;
}
@property (nonatomic ,strong) UITableView * personal_B0_TableView;

@end

@implementation personal_B0

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = RGB(241, 241, 241);
    self.navigationItem.title = @"个人资料";
    
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithIcon:@"back" highIcon:@"back" target:self action:@selector(back_back)];
    
    [self creatUI];
}
- (void)back_back
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

}

#pragma mark -creatUI-
- (void)creatUI
{
    self.personal_B0_TableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT +44) style:UITableViewStyleGrouped];
    self.personal_B0_TableView.delegate = self;
    self.personal_B0_TableView.dataSource =self;
    self.personal_B0_TableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.personal_B0_TableView.backgroundColor = RGB(241, 241, 241);
    [self.view addSubview:self.personal_B0_TableView];
    
}

#pragma mark ------- UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 3) {
        return 0;
    }
    return 8.0f;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 2) {
        return 1;
    }
    return 3;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        
        if (indexPath.row == 0) {
            return 60;
        }
        
    }
    return 45;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellID = @"cellID";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    
    UILabel *iconNameLabel;
    
    if (cell == nil) {
        
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellID];
        
        swich = [[UISwitch alloc]initWithFrame:CGRectMake(WIDTH-60, 5, 50, 40)];
        
        iconNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 200, cell.contentView.frame.size.height)];
        iconNameLabel.font = [UIFont systemFontOfSize:15.0f];
        iconNameLabel.textAlignment = NSTextAlignmentLeft;
        [cell.contentView addSubview:iconNameLabel];
        
        UIView *lineView2 = [[UIView alloc] initWithFrame:CGRectMake(0, cell.contentView.frame.size.height-1, WIDTH, 1)];
        lineView2.backgroundColor = [UIColor clearColor];
        [cell.contentView addSubview:lineView2];
        
        if (indexPath.row != 0) {
            UIView *lineView1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 1)];
            lineView1.backgroundColor = RGB(241, 241, 241);
            [cell.contentView addSubview:lineView1];
        }
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.accessoryType  = UITableViewCellAccessoryDisclosureIndicator;
    NSArray *iconName0 = @[@"头像",@"K歌号",@"昵称"];
    NSArray *iconName1 = @[@"手机号",@"现居地",@"生日"];
    //NSArray *iconName2 = @[@"修改密码",@"房间保密"];
    NSArray *iconName2 = @[@"修改密码"];
    
    if (indexPath.section == 0) {
        switch (indexPath.row) {
            case 1:
                cell.detailTextLabel.text = [NSString stringWithFormat:@"%@",[FileStorage NSUserDefault_getObjectForKey:User_id]];
                break;
            case 2:
                cell.detailTextLabel.text = [NSString stringWithFormat:@"%@",[FileStorage NSUserDefault_getObjectForKey:nickName]];
                break;
        }
        iconNameLabel.text = iconName0[indexPath.row];
        if (indexPath.row == 0) {
            iconImage=[[UIImageView alloc]initWithFrame:CGRectMake(WIDTH-100, 5, 50,50)];
            iconImage.layer.cornerRadius= iconImage.width/2;
            iconImage.layer.masksToBounds = YES;
            [iconImage setUserInteractionEnabled:YES];
            
            NSString * imageurl = [NSString stringWithFormat:@"%@",[FileStorage NSUserDefault_getObjectForKey:user_image]];
            
            if ([imageurl isEqualToString:@"001"]) {
                iconImage.image=[UIImage imageNamed:@"icon_person"];
            }
            else
            {
                [iconImage sd_setImageWithURL:[NSURL URLWithString:imageurl]];
            }
            UITapGestureRecognizer * PersonalTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(Personal_Change)];
            [iconImage addGestureRecognizer:PersonalTap];
            
            [cell.contentView addSubview:iconImage];
        }
        
    }
    else if (indexPath.section == 1)
    {
        if (indexPath.row==0) {
            cell.detailTextLabel.text = [NSString stringWithFormat:@"%@",[FileStorage NSUserDefault_getObjectForKey:user_phone]];
        }
        iconNameLabel.text = iconName1[indexPath.row];
    }
    else if (indexPath.section == 2)
    {
        if (indexPath.row==1) {
            cell.accessoryType  = UITableViewCellAccessoryNone;
            [cell.contentView addSubview:swich];
        }
        iconNameLabel.text = iconName2[indexPath.row];
    }
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:tableView.indexPathForSelectedRow animated:YES];
}


- (void)Personal_Change
{
    NSLog(@"更换头像");
    UpUserIconControllerViewController *upUseIcon = [UpUserIconControllerViewController new];
    [upUseIcon changeUserIconMethods:^(UIImage *image) {
        iconImage.image = image;
    }];
    [self.navigationController pushViewController:upUseIcon animated:YES];
}


@end
